﻿using Simple.Admin.System.Enums;
using Simple.Admin.System.Identity;
using System.ComponentModel.DataAnnotations;
using Volo.Abp.Features;
using Volo.Abp.Identity;
using Volo.Abp.Localization;
using Volo.Abp.ObjectExtending;
using Volo.Abp.Threading;

namespace Simple.Admin;

public static class AdminModuleExtensionConfigurator
{
    private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner();

    public static void Configure()
    {
        OneTimeRunner.Run(() =>
        {
            ConfigureExistingProperties();
            ConfigureExtraProperties();
        });
    }

    private static void ConfigureExistingProperties()
    {
        /* You can change max lengths for properties of the
         * entities defined in the modules used by your application.
         *
         * Example: Change user and role name max lengths

           AbpUserConsts.MaxNameLength = 99;
           IdentityRoleConsts.MaxNameLength = 99;

         * Notice: It is not suggested to change property lengths
         * unless you really need it. Go with the standard values wherever possible.
         *
         * If you are using EF Core, you will need to run the add-migration command after your changes.
         */
    }

    private static void ConfigureExtraProperties()
    {
           ObjectExtensionManager.Instance.Modules()
              .ConfigureIdentity(identity =>
              {
                  identity.ConfigureUser(user =>
                  {
                      user.AddOrUpdateProperty<string?>(
                          IdentityUserExtensionPropertyConst.User.AvatarUrl,
                          property =>
                          {
                              property.Attributes.Add(new StringLengthAttribute(255) {MinimumLength = 4});
                          }
                      );
                      user.AddOrUpdateProperty<GenderEnum>(
                          IdentityUserExtensionPropertyConst.User.Gender,
                          property =>
                          {
                          }
                      );
                      user.AddOrUpdateProperty<string?>(
                            IdentityUserExtensionPropertyConst.User.Introduction,
                            property =>
                            {
                                property.Attributes.Add(new StringLengthAttribute(255) {MinimumLength = 4});
                            }
                      );
                      // 省级（如 "广东省"）
                      user.AddOrUpdateProperty<string>(
                          IdentityUserExtensionPropertyConst.User.Province,
                          property =>
                          {
                              property.DefaultValue=string.Empty;
                              property.Attributes.Add(new StringLengthAttribute(50));
                              property.DisplayName = new FixedLocalizableString("省份");
                          }
                      );

                      // 市级（如 "深圳市"）
                      user.AddOrUpdateProperty<string>(
                          IdentityUserExtensionPropertyConst.User.City,
                          property =>
                          {
                              property.DefaultValue = string.Empty;
                              property.Attributes.Add(new StringLengthAttribute(50));
                              property.DisplayName = new FixedLocalizableString("城市");
                          }
                      );

                      // 区级（如 "南山区"）
                      user.AddOrUpdateProperty<string>(
                          IdentityUserExtensionPropertyConst.User.District,
                          property =>
                          {
                              property.DefaultValue = string.Empty;
                              property.Attributes.Add(new StringLengthAttribute(50));
                              property.DisplayName = new FixedLocalizableString("区县");
                          }
                      );

                      // 详细地址（如 "科技园路123号"）
                      user.AddOrUpdateProperty<string>(
                          IdentityUserExtensionPropertyConst.User.Address,
                          property =>
                          {
                              property.DefaultValue = string.Empty;
                              property.Attributes.Add(new RequiredAttribute()); // 必填
                              property.Attributes.Add(new StringLengthAttribute(255));
                              property.DisplayName = new FixedLocalizableString("详细地址");
                          }
                      );
                  });
              });
    }
}
